﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Techstar.Dialog;
using System.Data;
using System.Collections.ObjectModel;
using SmartMD.DataAccess.Oracle;
using SmartMD.DataModel.Entity;
using SmartMD.Manager.Utils;
using System.Collections;
using SmartMD.Manager.Common;

namespace SmartMD.Manager.Patient
{
    /// <summary>
    /// Interaction logic for QueryInfoEditPad.xaml
    /// </summary>
    public partial class QueryInfoEditPad : UserControl
    {
        public QueryInfoEditPad()
        {
            InitializeComponent();
        }

        public QueryInfoPanel Owner;

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            bool hasWhere = false;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append("select DISTINCT  t.* ");
            //sb.Append("select t.PATIENTID, t.FIRSTFOLLOWUP, t.NAME, t.HOSPITALNO, t.CLINICNO, t.SEX, t.BIRTHDAY, t.AREA, t.TELEPHONE, t.ADDRESS");
            sb.Append(" from DUO_PATIENT t ");
            if (this.cbSubject.IsChecked == true)  //所属课题
            {
                sb.Append(" ,DUO_SPECIMENS t2");
            }
            if (this.cbDiagnose.IsChecked == true)  //疾病诊断
            {
                sb.Append(" ,DUO_FOLLOWUP_INFO t3 ,DUO_DIAGNOSE_GROUP t4");
            }
   
            if (this.cbSubject.IsChecked == true)  //所属课题
            {
                if (!hasWhere)
                {
                    sb.Append(" where ");
                    hasWhere = true;
                }
                else
                {
                    sb.Append(" and ");
                }
                String subjectTxt = this.txtSubject.Text;
                sb.Append(" t.PATIENTID = t2.PATIENTID");
                sb.Append(" and t2.SUBJECT = '" + subjectTxt + "'");
            }
            if (this.cbDiagnose.IsChecked == true)  //疾病诊断
            {
                if (!hasWhere)
                {
                    sb.Append("  where ");
                    hasWhere = true;
                }
                else
                {
                    sb.Append(" and ");
                }
                String diagnoseTxt = this.txtDiagnose.Text;
                sb.Append(" t.PATIENTID = t3.PATIENTID and t3.ID = t4.FOLLOWUPID");
                sb.Append(" and t4.DIAGNOSE = '" + diagnoseTxt + "'");
            }
            if (this.cbPatientName.IsChecked == true) //患者姓名
            {
                if (!hasWhere)
                {
                    sb.Append(" where ");
                    hasWhere = true;
                }
                else {
                    sb.Append(" and ");
                }
                String idTxt = this.txtPatientName.Text;
                sb.Append(" t.NAME = '" + idTxt + "'");
            }
            if (this.cbSex.IsChecked == true)  //患者性别
            {
                if (!hasWhere)
                {
                    sb.Append(" where ");
                    hasWhere = true;
                }
                else
                {
                    sb.Append(" and ");
                }
                String sexTxt = this.txtSex.Text;
                sb.Append(" t.SEX= '" + sexTxt + "'");
            }
            if (this.cbArea.IsChecked == true) //地域
            {
                if (!hasWhere)
                {
                    sb.Append(" where ");
                    hasWhere = true;
                }
                else
                {
                    sb.Append(" and ");
                }
                String areaTxt = this.txtArea.Text;
                sb.Append(" t.AREA = '" + areaTxt + "'");
            }
            
            if (this.cbClinicNo.IsChecked == true)  //门诊号
            {
                if (!hasWhere)
                {
                    sb.Append(" where ");
                    hasWhere = true;
                }
                else
                {
                    sb.Append(" and ");
                }
                String clinicNoTxt = this.txtClinicNo.Text;
                sb.Append(" t.CLINICNO = '" + clinicNoTxt + "'");
            }
            if (this.cbHospitalNo.IsChecked == true)  //住院号
            {
                if (!hasWhere)
                {
                    sb.Append(" where ");
                    hasWhere = true;
                }
                else
                {
                    sb.Append(" and ");
                }
                String hospitalNoTxt = this.txtHospitalNo.Text;
                sb.Append(" t.HOSPITALNO = '" + hospitalNoTxt + "'");
            }
            if (this.cbFollowupNo.IsChecked == true)  //住院号
            {
                if (!hasWhere)
                {
                    sb.Append(" where ");
                    hasWhere = true;
                }
                else
                {
                    sb.Append(" and ");
                }
                String followupNoTxt = this.txtFollowupNo.Text;
                sb.Append(" t.FOLLOWUPNO = '" + followupNoTxt + "'");
            }
            if (this.cb_timerange.IsChecked == true)  //时间范围
            {
                if (!hasWhere)
                {
                    sb.Append(" where ");
                    hasWhere = true;
                }
                else
                {
                    sb.Append(" and ");
                }
                DateTime startTime = DateTime.Parse(this.patientStartTime.Text);
                DateTime endTime = DateTime.Parse(this.patientEndTime.Text);
                sb.Append(" t.FIRSTFOLLOWUP >= " + "to_date ( '" + startTime.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' )");
                sb.Append(" and t.FIRSTFOLLOWUP <= " + "to_date ( '" + endTime.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' )");
            }

            DataTable dt = ConnectDatabase.getInstance().Select(sb.ToString());
            ObservableCollection<PatientInfo> resultList = new ObservableCollection<PatientInfo>();
            PatientInfo item = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    item = new PatientInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        item.setValue(clmName, dt.Rows[j][clmName]);                  
                    }
                    item.State = DataModel.RecordState.Normal;
                    resultList.Add(item);
                }
                this.Owner.ucPatientQueryList.PatientList = resultList;
            }
            else
            {
                if (this.Owner.ucPatientQueryList.PatientList != null)
                {
                    this.Owner.ucPatientQueryList.PatientList.Clear();
                }
                TaskDialogEx.Warning("查询失败", "未查到您所查询的患者信息！");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.txtArea.Text = "";
            this.txtClinicNo.Text = "";
            this.txtDiagnose.Text = "";
            this.txtHospitalNo.Text = "";
            this.txtPatientName.Text = "";
            this.txtSex.Text = "";
            this.txtSubject.Text = "";
        }

        
        private void txtSex_Initialized(object sender, EventArgs e)
        {
            List<ComboBoxValueItem> datasource = new List<ComboBoxValueItem>();
            datasource.Add(new ComboBoxValueItem { Name = "男", Value = "male" });
            datasource.Add(new ComboBoxValueItem { Name = "女", Value = "female" });
            (sender as ComboBox).ItemsSource = datasource;
        }

        public void InitAllComboxValue()
        {
            string key;
            object sender;
            foreach (DictionaryEntry de in _cmbHashtable)
            {
                key = de.Key.ToString();
                sender = de.Value as object;
                ComboxUtil.InitializeComboxByDict(sender, key);
            }
        }

        private Hashtable _cmbHashtable = new Hashtable();
        private void txtArea_Initialized(object sender, EventArgs e)
        {
            _cmbHashtable.Add("患者地域", sender);
            ComboxUtil.InitializeComboxByDict(sender, "患者地域");
        }

        private void txtDiagnose_Initialized(object sender, EventArgs e)
        {
            _cmbHashtable.Add("疾病诊断", sender);
            ComboxUtil.InitializeComboxByDict(sender, "疾病诊断");
        }
    }
}
